package com.project.market.controller;

import com.project.market.db.domain.MarketCollect;
import com.project.market.service.AdminCollertService;
import com.project.market.service.impl.AdminCollectServiceImpl;
import com.project.market.util.JacksonUtil;
import com.project.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/collect/*")
public class AdminCollectController extends HttpServlet {

    AdminCollertService adminCollertService = new AdminCollectServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String op = req.getRequestURI().replace(req.getContextPath() + "/admin/collect/", "");
        if("list".equals(op)){
            list(req,resp);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String userId = req.getParameter("userId");
        String valueId = req.getParameter("valueId");

        List<MarketCollect> list = adminCollertService.list(page,limit,sort,order,userId,valueId);
        if(list == null){
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.fail(402,"参数值错误")));
        }else {
        Object ok = ResponseUtil.okList(list);
        String s = JacksonUtil.writeValueAsString(ok);
        resp.getWriter().println(s);}
    }
}
